package com.fullstackdaytwo.registerlogin.service;

import com.fullstackdaytwo.registerlogin.enums.RoleEnum;
import com.fullstackdaytwo.registerlogin.pojo.AbstractUser;
import com.fullstackdaytwo.registerlogin.pojo.Admin;
import com.fullstackdaytwo.registerlogin.pojo.Customer;
import com.fullstackdaytwo.registerlogin.pojo.User;
import com.fullstackdaytwo.registerlogin.util.FileOperator;

/**
 * @Description: 登陆注册实现类
 * @author: Aron
 * @date: 2021.10.15
 */

public class UserServiceImpl implements UserService{
    /**
    * @Description: 注册方法
    * @Param: []
    * @return: boolean
    * @Author: Aron
    * @Date: 2021/10/15
    */

    @Override
    public boolean signUp(String[] strs) {
        if (strs[0].equals(String.valueOf(RoleEnum.ADMIN.getCode()))) {
            AbstractUser user = new Admin(strs[0], strs[1], strs[2]);
            FileOperator.fileWriter(user);
            return true;
        } else if (strs[0].equals(String.valueOf(RoleEnum.CUSTOMER.getCode()))){
            AbstractUser user = new Customer(strs[0], strs[1], strs[2]);
            FileOperator.fileWriter(user);
            return true;
        }
        return false;
    }
/**
* @Description: 登录方法
* @Param: []
* @return: boolean
* @Author: Aron
* @Date: 2021/10/15
 * @param presentStr
 */
    @Override
    public String[] signIn(String[] presentStr) {
        AbstractUser user = new User(presentStr[0], presentStr[1]);
        String actualStrs = FileOperator.fileReader(user);
        if (actualStrs != null) {
            String[] splitStrs = actualStrs.split(",");
            if (user.getUserName().equals(splitStrs[1]) && user.getPassWord().equals(splitStrs[2])) {
                return splitStrs;
            } else {
                return null;
            }
        }
        return null;

    }
}
